package com.teame.readbaby;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.teame.readbaby.BaseActivity.DataCallback;
import com.teame.readbaby.dao.AddressDao;
import com.teame.readbaby.parser.BooleanParser;
import com.teame.readbaby.util.CommonUtil;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.vo.AddressListInfo;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddressUpdataActivity extends BaseActivity {

	private EditText add_address_name_edit;
	private EditText add_address_mobile_edit;
	private EditText add_address_tel_edit;
	private TextView add_address_province_name_text;
	private TextView add_address_city_name_text;
	private TextView add_address_area_name_text;
	private EditText add_address_detail_edit;
	private EditText add_address_zipcode_edit;
	private Button save_address_button;
	private Button cancel_address_button;
	private AddressDao dao;
	
	
	public void onClick(View v) {
		if(v.getId()==R.id.save_address_button){
			Map<String, String> params=new HashMap<String, String>();
			params.put("name",add_address_name_edit.getText().toString());
			params.put("phonenumber",add_address_mobile_edit.getText().toString());
			params.put("fixedtel",add_address_tel_edit.getText().toString());
//			String province= add_address_province_name_text.getText().toString();
//			String city= add_address_city_name_text.getText().toString();
//			String area=add_address_area_name_text.getText().toString();
//			String areaid=new AddressDao().getAreaid(province,city,area);
			//params.put("areaid",areaid);
			params.put("areaid",010305+"");//测试使用
			params.put("areadetail",add_address_detail_edit.getText().toString());
			params.put("zipcode",add_address_zipcode_edit.getText().toString());
			boolean isok=true;
			for (Map.Entry<String, String> entry : params.entrySet()) {
				if(TextUtils.isEmpty(entry.getValue())){
					isok=false;
				}
			}
			if(isok){//文本输入没有问题
				String url = ConstantValue.HOST + ConstantValue.ADD_ADDRESS;
				super.getDataFromServer(url, params, new BooleanParser(), new DataCallback<Boolean>() {

					@Override
					public void processData(Boolean paramObject,
							boolean paramBoolean) {
						if(paramObject){
							//CommonUtil.showInfoDialog(AddressUpdataActivity.this, "添加成功");
							CommonUtil.showInfoDialog(AddressUpdataActivity.this, "修改成功", "提示", "确认", new OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									AddressUpdataActivity.this.finish();
								}
							});
							//AddressUpdataActivity.this.finish();
						}else{
							//CommonUtil.showInfoDialog(AddressUpdataActivity.this, "添加失败");
							CommonUtil.showInfoDialog(AddressUpdataActivity.this, "修改失败");
							CommonUtil.showInfoDialog(AddressUpdataActivity.this, "修改失败", "提示", "确认", new OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									AddressUpdataActivity.this.finish();
								}
							});
						}
					}
				});
			}else{
				CommonUtil.showInfoDialog(this, "您输入的数据为空或地址输入不正确");
			}
			
		}else if(v.getId()==R.id.cancel_address_button){
			this.finish();
		}
	}

	@Override
	protected void findViewById() {
		add_address_name_edit=(EditText) this.findViewById(R.id.add_address_name_edit);
		add_address_mobile_edit=(EditText) this.findViewById(R.id.add_address_mobile_edit);
		add_address_tel_edit=(EditText) this.findViewById(R.id.add_address_tel_edit);
		add_address_province_name_text=(TextView) this.findViewById(R.id.add_address_province_name_text);
		add_address_city_name_text=(TextView) this.findViewById(R.id.add_address_city_name_text);
		add_address_area_name_text=(TextView) this.findViewById(R.id.add_address_area_name_text);
		add_address_detail_edit=(EditText) this.findViewById(R.id.add_address_detail_edit);
		add_address_zipcode_edit=(EditText) this.findViewById(R.id.add_address_zipcode_edit);
		cancel_address_button=(Button) this.findViewById(R.id.cancel_address_button);
		save_address_button=(Button) this.findViewById(R.id.save_address_button);
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.add_address_activity);
		dao = new AddressDao();
	}

	@Override
	protected void processLogic() {
		Serializable extra = getIntent().getSerializableExtra("info");
		if(extra!=null&&extra instanceof AddressListInfo){
			AddressListInfo info=(AddressListInfo) extra;
			add_address_name_edit.setText(info.getName());
			add_address_mobile_edit.setText(info.getPhonenumber());
			//add_address_tel_edit.setText();
			add_address_province_name_text.setText(dao.getAreaid(info.getProvinceName()));
			add_address_city_name_text.setText(dao.getAreaid(info.getCityid()));
			add_address_area_name_text.setText(dao.getAreaid(info.getAreaid()));
			add_address_detail_edit.setText(info.getAreadetail());
			add_address_zipcode_edit.setText(info.getZipcode());
		}
	}

	@Override
	protected void setListener() {
		cancel_address_button.setOnClickListener(this);
		save_address_button.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View paramView) {
		
		onClick(paramView);
	}
}
